import {Component} from "@angular/core";
import {AlertController, NavController, ToastController} from "ionic-angular";
import {Storage} from "@ionic/storage";
import {UserService} from "../../service/user/user.service";
import {FeedbackPage} from "../feedback/feedback";
import {UserData} from "../../common/user.data";
import {NetSettingPage} from "../netsetting/netsetting";
import {ThemeableBrowser} from '@ionic-native/themeable-browser';
import {UrlData} from "../../common/urldata";


@Component({
  selector: 'page-setting',
  templateUrl: 'setting.html',
  providers: [UserService]
})
export class SettingPage {


  constructor(private storage: Storage,
              private navCtrl: NavController,
              private user: UserData,
              private themeableBrowser: ThemeableBrowser,
              public alertCtrl: AlertController,
              public urlData: UrlData,
              private userService: UserService,
              private toast: ToastController) {
  }

  feedbackClick() {
    this.navCtrl.push(FeedbackPage);
  }

  aboutUsClick() {
    this.themeableBrowser.create(this.urlData.baseUrl + "/app/about", "_self", {
      statusbar: {
        color: '#ffffffff'
      },
      toolbar: {
        height: 44,
        color: '#f0f0f0ff'
      },
      title: {
        color: '#003264ff',
        showPageTitle: true
      },
      closeButton: {
        wwwImage: 'assets/img/icons/close16.png',
        wwwImagePressed: 'assets/img/icons/close16.png',
        // image: 'close',
        // imagePressed: 'close_pressed',
        align: 'left',
        event: 'closePressed'
      },
    });
  }

  checkUpdateClick() {
  }

  netSetting() {
    this.navCtrl.push(NetSettingPage)
  }

  logoutClick() {
    console.log('退出账号');
    this.alertCtrl.create({
      title: '是否确认退出?',
      buttons: [{
        text: '取消',
        role: 'cancel',
        handler: () => {
          console.log('Cancel clicked');
        }
      }, {
        text: '确认',
        handler: () => {
          this.storage.get("oauthToken").then(value => {
            this.userService.logout(value).subscribe(res => {
              if (res.status === "SUCCESS") {
                this.storage.remove("user");
                this.storage.remove("oauthToken");
                this.user.token = null;
                this.user.user = null;
              } else {
                this.toast.create({
                  message: res.message,
                  duration: 2000,
                  position: 'top'
                }).present();
              }
            }, () => {
              this.toast.create({
                message: "退出失败,请重试!",
                duration: 2000,
                position: 'top'
              }).present();
            });
          });
        }
      }]
    }).present();
  }
}
